package com.tianqu.tms.www.transfer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransMarketCode;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class TransferSeniorChangeJYPInputAction extends TmsAction {
	private Logger logger = Logger.getLogger(TransferSeniorChangeJYPInputAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String serial = "";
		if(request.getParameter("serial")!=null)
		{
			serial = request.getParameter("serial");
		}else{
			serial = (String)uc.getAttribute("serial");
		}
		uc.setAttribute("serial", serial);
		//根据流水号查询所有车辆信息
		TransferRgInputViewHelper view = new TransferRgInputViewHelper();
		if(uc.getAttribute(TmsConstants.VIEWHELP)!=null)
		{
			view = (TransferRgInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		}
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		accept = accept.getAcceptInfoBySerial(serial,"");
		Car car = new Car();
		Codes code = Codes.getInstance();
		TransferPersonalCarInfo person = new TransferPersonalCarInfo();
		if(accept.getMarketType() == 1)
		{
			//库存
			car = car.findCarBySerial(accept.getBasicCarId());
			view = toViewHelper(car, view);
		}
		if(accept.getMarketType() == 2)
		{
			//非库存
			person = person.getPersonalCarInfoBySerial(accept.getBasicCarId());
			view = toViewHelper1(person, view);
		}
		/******过户信息部分 */
		view.setSerial(accept.getSerial());
		view.setStatus(String.valueOf(accept.getStatus()));
		view.setStatusName(code.getItemValueByItemIdAndItemCode(Codes.ACCEPT_STATUS,accept.getStatus()));
		
		view.setTransferType(String.valueOf(accept.getTransferType()));
		view.setCarMarketIn(accept.getCarMarketIn());
		view.setMoveOutProvince(accept.getMoveOutProvince());
		view.setGainType(code.getItemValueByItemIdAndItemCode(Codes.GAIN_TYPE, accept.getGainType()));
		view.setNewOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER, accept.getNewOwnerType()));
		view.setNewCarOwner(accept.getNewcarOwner());
		view.setNewcarOwnerIdCardNo(accept.getNewcarOwnerIdCardNo());
		view.setNewOwnerOrgNo(accept.getNewOwnerOrgNo());
		view.setNewOwnerAddress(accept.getNewOwnerAddress());
		view.setNewOwnerPhone(accept.getNewOwnerPhone());
		view.setNewOwnerTelPhone(accept.getNewOwnerTelPhone());
		view.setZipCode(accept.getNewOwnerZipCode());
		view.setMailAddress(accept.getNewOwnerMailAddress());
		view.setNewAgentName(accept.getNewAgentName());
		view.setNewAgentOwnerIdCardNo(accept.getNewAgentOwnerIdCardNo());
		view.setNewAgentAddress(accept.getNewAgentAddress());
		view.setNewAgentPhone(accept.getNewAgentPhone());
		view.setOwnerType(String.valueOf(accept.getOwnerType()));
		view.setMoveOutProvince(accept.getMoveOutProvince());
		view.setMoveOutCity(accept.getMoveOutCity());
		view.setNewOwnerType(String.valueOf(accept.getNewOwnerType()));
		

		if(accept.getPrice() > 0)
		{
			view.setPrice(StringUtil.addDH(String.valueOf(accept.getPrice()))); 
		}else{
			view.setPrice(""); 
		}
		view.setTaxNo(accept.getTaxNo());
		view.setTransNo(accept.getTransNo());
		if(accept.getTransMarket()!=null && !"".equals(accept.getTransMarket()))
		{
			TransMarketCode transMarket = new TransMarketCode();
			transMarket = transMarket.getTransMarketCodeInfo(accept.getTransMarket());
			if(transMarket!=null)
			{
				view.setTransMarket(transMarket.getMarketName());
			}else{
				view.setTransMarket("");
			}
		}else{
			view.setTransMarket("");
		}
		
		//这块需要添加一些下拉列表 车辆类型 二手车市场
		view.setCarTypeList(code.getCodeslistByItemId(Codes.VEHICLE_TYPE));// 车辆类型
		TransMarketCode transCode = new TransMarketCode();
		view.setTransMarketList(transCode.loadAllTransMarketCode());
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		request.setAttribute(TmsConstants.VIEWHELP,view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private TransferRgInputViewHelper toViewHelper(Car entity,TransferRgInputViewHelper view) throws TmsDaoException {

		Codes code = Codes.getInstance();
		view.setCarName(entity.getCarName());
		view.setAttribute(code.getItemValueByItemIdAndItemCode(Codes.ATTACHMENT_PROPERTY, entity.getAttribute()));
		view.setBrandId(entity.getBrandId());
		view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, entity.getCardType()));
		view.setLicensePlate(entity.getLicensePlate());
		view.setCarOwner(entity.getCarOwner());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setCarType(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, entity.getCarType()));
		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE, entity.getCarUse()));
		view.setColor(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR, entity.getColor()));
		view.setCountryStyle(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY, entity.getCountryStyle()));
		view.setDisplacement(String.valueOf(entity.getDisplacement()));
		view.setEngine(entity.getEngine());
		view.setFuelType(code.getItemValueByItemIdAndItemCode(Codes.FUEL_TYPE, entity.getFuelType()));
		view.setGearbox(code.getItemValueByItemIdAndItemCode(Codes.GEARBOX_TYPE, entity.getGearbox()));
		view.setJqx(entity.getJqx());
		if(entity.getLoadquality() > 0)
		{
			view.setLoadquality(String.valueOf(entity.getLoadquality()));
		}else{
			view.setLoadquality("");
		}
		view.setBrandId(entity.getBrandId());
		view.setVendorId(entity.getVendorId());		
		view.setCarStyleId(entity.getCarStyleId());
		if(entity.getMileage() > 0)
		{
			view.setMileage(StringUtil.addDH(String.valueOf(entity.getMileage())));
		}else{
			view.setMileage("");
		}
		view.setModelCode(entity.getModelCode());
		view.setOwnerAddress(entity.getOwnerAddress());
		view.setOwnerOrgNo(entity.getOwnerOrgNo());
		view.setOwnerPhone(entity.getOwnerPhone());
		//view.setOwnerType(String.valueOf(entity.getOwnerType()));
		if(entity.getPassenger()>0)
		{
			view.setPassenger(String.valueOf(entity.getPassenger()));
		}else{
			view.setPassenger("");
		}		//view.setPrice(StringUtil.addDH(String.valueOf(entity.getPrice())));

		view.setRegDate(entity.getRegDate());
		view.setRegisterCardNo(entity.getRegisterCardNo());
		view.setSendCardDate(entity.getSendCardDate());
		
		view.setVendorCode(entity.getVendorCode());
		view.setVin(entity.getVin());
		view.setYearCarefulPeriod(entity.getYearCarefulPeriod());
		
		return view;

	}
	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private TransferRgInputViewHelper toViewHelper1(TransferPersonalCarInfo entity,TransferRgInputViewHelper view) throws TmsDaoException {

		Codes code = Codes.getInstance();
		view.setCarName(entity.getCarName());
		view.setAttribute(code.getItemValueByItemIdAndItemCode(Codes.ATTACHMENT_PROPERTY, entity.getAttribute()));
		view.setBrandId(entity.getBrandId());
		view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, entity.getCardType()));
		view.setLicensePlate(entity.getLicensePlate());
		view.setCarOwner(entity.getCarOwner());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setCarType(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, entity.getCarType()));
		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE, entity.getCarUse()));
		view.setColor(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR, entity.getColor()));
		view.setCountryStyle(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY, entity.getCountryStyle()));
		view.setDisplacement(String.valueOf(entity.getDisplacement()));
		view.setEngine(entity.getEngine());
		view.setFuelType(code.getItemValueByItemIdAndItemCode(Codes.FUEL_TYPE, entity.getFuelType()));
		view.setGearbox(code.getItemValueByItemIdAndItemCode(Codes.GEARBOX_TYPE, entity.getGearbox()));
		view.setJqx(entity.getJqx());
		if(entity.getLoadquality() > 0)
		{
			view.setLoadquality(String.valueOf(entity.getLoadquality()));
		}else{
			view.setLoadquality("");
		}
		view.setBrandId(entity.getBrandId());
		view.setVendorId(entity.getVendorId());		
		view.setCarStyleId(entity.getCarStyleId());
		if(entity.getMileage() > 0)
		{
			view.setMileage(StringUtil.addDH(String.valueOf(entity.getMileage())));
		}else{
			view.setMileage("");
		}
		view.setModelCode(entity.getModelCode());
		view.setOwnerAddress(entity.getOwnerAddress());
		view.setOwnerOrgNo(entity.getOwnerOrgNo());
		view.setOwnerPhone(entity.getOwnerPhone());
		//view.setOwnerType(String.valueOf(entity.getOwnerType()));
		view.setPassenger(String.valueOf(entity.getPassenger()));
		//view.setPrice(StringUtil.addDH(String.valueOf(entity.getPrice())));

		view.setRegDate(entity.getRegDate());
		view.setRegisterCardNo(entity.getRegisterCardNo());
		view.setSendCardDate(entity.getSendCardDate());
		
		view.setVendorCode(entity.getVendorCode());
		view.setVin(entity.getVin());
		view.setYearCarefulPeriod(entity.getYearCarefulPeriod());
		
		return view;
	}	
}
